import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by guojian on 13/09/2017.
 */
public class Chrome {

    public static void main(String[] args) throws InterruptedException, IOException {
        String exePath = "/usr/local/bin/chromedriver";
        System.setProperty("webdriver.chrome.driver", exePath);

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless");
        options.addArguments("--no-sandbox");
        options.addArguments("--disable-gpu");


        DesiredCapabilities chromeDesiredCapabilities = DesiredCapabilities.chrome();
        chromeDesiredCapabilities.setCapability(ChromeOptions.CAPABILITY, options);

        WebDriver driver = new ChromeDriver(chromeDesiredCapabilities);
        driver.get("http://baidu.com");
        Thread.sleep(3000);

        File screenShotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(screenShotFile, new File( getCurrentDateTime() + ".jpg"));

        System.out.println(driver.getCurrentUrl());
        driver.quit();
    }

    public static String getCurrentDateTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd_HHmmss");//设置日期格式
//System.out.println(df.format(new Date()));
        return df.format(new Date());
    }
}